package com.sanjeet.homecontrol;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpSocketHandler {
	
	
	private Socket skt;
	private PrintWriter out;
	private BufferedReader inFromServer;
	
	public TcpSocketHandler() throws UnknownHostException, IOException
	{
		 this.skt = new Socket("192.168.1.221", 5678);		
		 this.out = new PrintWriter(skt.getOutputStream(), true);
		 this.inFromServer = new BufferedReader(new InputStreamReader(skt.getInputStream()));
	}
	
	public TcpSocketHandler(String string, int i) throws UnknownHostException, IOException {
		
		 this.skt = new Socket(string, i);
		 this.out = new PrintWriter(skt.getOutputStream(), true);
		 this.inFromServer = new BufferedReader(new InputStreamReader(skt.getInputStream()));
	}

	public void send(String str)
	{
		this.out.println(str);
	}
	
	public String reciever()
	{
		try {
			String sentence = this.inFromServer.readLine();
			System.out.println(sentence);
			
			return sentence;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "NO";
	}
	
	public void disconnect()
	{
		this.out.close();
		try {
			this.skt.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
}